/*
 * Hotel.java
 *
 * This class stores details about rooms booked by the customer.
 *
 * 
 */

package com.javacore.day9.hotel;

import java.util.InputMismatchException;
import java.util.Scanner;

/**
 * Hotel class is used to book rooms, change the total number of rooms
 * in a hotel and display the reservation details.
 * @author 
 */
public class Hotel {
    
    /**
     * Creates a new instance of Hotel.
     */
    public Hotel() {
        
        this.capacity = 10;
        reservations = new RoomReservation[maximumReservations];
    }
    
    /**
     * Method to display the Reservation Details.
     */
    public void displayReservationDetails(RoomReservation item) {
        
        System.out.println("\nYour room reservation details are:\n");
        System.out.format("%1$14s  %2$-25s %3$s\n", "Reservation ID",
                "Customer Name", "Number of rooms booked");
        System.out.println("-----------------------------------------------" +
                "---------------");
        System.out.format("%1$-14d  %2$-25s %3$d\n", item.reservationID,
                item.customerName, item.roomsBooked);
    }
    
    /**
     * Method to set the total number of rooms in the hotel.
     */
    public void setCapacity()  {
        
        Scanner input = new Scanner(System.in);
        
        // Temporary variable to store the new capacity of the hotel
        int temp = 0;
        
        try {
            
            // Accept the new capacity of the hotel
            System.out.println("Enter new capacity of the hotel:");
            temp = input.nextInt();
            
            // If capacity is zero or negative value, throw an exception
            if(temp <= 0) {
                throw new Exception("Enter a non-zero positive number for capacity.");
            }
            
            // Store the new capacity
            this.capacity += temp;
        } catch (InputMismatchException e) {
            System.out.println("Enter a non-zero positive number for capacity.");
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
    
    /**
     * Method to book rooms.
     */
    public void bookRooms() {
        
        Scanner input = new Scanner(System.in);
        try {
            
            System.out.println("Enter customer name:");
            String customerName = input.nextLine();
            
            System.out.println("How many rooms do you want to book?");
            int rooms = input.nextInt();
            
            // If the value of number of rooms requested is zero or negative,
            // throw an exception
            if(rooms <= 0){
                throw new Exception("Please enter a positive non-zero value " +
                        "for number of rooms.");
            }
            
            // Compute the number of rooms available for booking
            int available = capacity - roomsOccupied;
            
            // If the number of requested rooms is greater than the available
            // number of rooms, throw the user-defined exception,
            // RoomsNotAvailableException; else process the booking request
            if (rooms > available) {
                throw new RoomsNotAvailableException(available);
            } else {
                
                // Create an instance of class RoomReservation to store
                // reservation details
                RoomReservation item = new RoomReservation();
                System.out.println("capacity= " + capacity);
                System.out.println("roomsOccupied= " + roomsOccupied);
                // Store the reservation details
                item.reservationID = nextReservationID++;
                item.customerName = customerName;
                item.roomsBooked = rooms;
                
                // Store the reservation details in the array
                // and increment the variable nextReservation
                reservations[nextReservation++] = item;
                
                // Update the number of rooms occupied
                roomsOccupied += rooms;
                
                System.out.println("Room booked successfully.");
                displayReservationDetails(item);
                
            }
        } catch(ArrayIndexOutOfBoundsException e) {
            System.out.println("Exception occurred - Could not book a room. " +
                    "The reservations are full.");
        } catch (RoomsNotAvailableException e) {
            System.out.println("Exception occurred - " + e.getMessage());
        } catch (InputMismatchException e) {
            System.out.println("Exception occurred - Data type mismatch. " +
                    "Enter non-zero numeric value.");
        } catch (Exception e) {
            System.out.println("Exception occurred - " + e.getMessage());
        }
    }
    
    /**
     * Instance variable to store the total number of rooms in the hotel.
     */
    private int capacity;
    
    /**
     * Instance variable to store the total number of rooms occupied
     * by customers.
     */
    private int roomsOccupied;
    
    /**
     * Instance variable indicating the location in the array where the next
     * reservation details should be stored.
     */
    private int nextReservation = 0;
    
    /**
     * Instance variable to store the reservation ID.
     */
    private int nextReservationID = 1;
    
    /**
     * Constant specifying the maximum number of reservations
     * allowed.
     */
    private final int maximumReservations = 3;
    
    /**
     * An array to store room reservation details.
     */
    private RoomReservation[] reservations;
}
